let section = new Section()
let treatmentSession = new TreatmentSession()
let user = new User()
let category = new Category()
let language = new Language()
let sectionFlow = new SectionFlow()
let question = new Question()
let sessionInstances = new SessionInstances()
let questionInstances = new QuestionnaireInstances()
let platform = getUrlParameter('platform') || 'user'
let defaultRoute =    {
        path: '/',
        redirect: platform==='user'?'/explore':'/home'
    }
let init = () => {

        let routers = [
            defaultRoute,
            {
                path: '/home',
                name: 'home',
                component: homeComponent,
                meta: {
                    keepAlive: false

                }
            },
            {
                path: '/recorder',
                name: 'recorder',
                component: recorderComponent,
                meta: {
                    keepAlive: false

                }
            },
            {
                path: '/createSection',
                name: 'createSection',
                component: createSectionComponent,
                meta: {
                    keepAlive: false

                }
            },
            {
                path: '/sections',
                name: 'sections',
                component: sectionsComponent,
                meta: {
                    keepAlive: false

                }
            },
            {
                path: '/description',
                name: 'description',
                component: descriptionComponent,
                meta: {
                    keepAlive: false

                }
            },
            {
                path: '/explore',
                name: 'explore',
                component: exploreComponent,
                meta: {
                    keepAlive: false

                }
            },
            {
                path: '/detail',
                name: 'detail',
                component: detailComponent,
                meta: {
                    keepAlive: false
                }
            },
            {
                path: '/manager',
                name: 'manager',
                component: managerComponent,
                meta: {
                    keepAlive: false
                }
            },
            {
                path: '/library',
                name: 'Library',
                component: libraryComponent,
                meta: {
                    keepAlive: false
                }
            },
            {
                path: '/questions',
                name: 'questions',
                component: questionsComponent,
                meta: {
                    keepAlive: false
                }
            },
            {
                path: '/result',
                name: 'result',
                component: resultComponent,
                meta: {
                    keepAlive: false
                }
            },
            {
                path: '/login',
                name: 'login',
                component: loginComponent,
                meta: {
                    keepAlive: false
                }
            },
            {
                path: '/register',
                name: 'register',
                component: registerComponent,
                meta: {
                    keepAlive: false
                }
            },
            {
                path: '/profile',
                name: 'profile',
                component: profileComponent,
                meta: {
                    keepAlive: false
                }
            },
            {
                path: '/history',
                name: 'history',
                component: historyComponent,
                meta: {
                    keepAlive: false
                }
            },
            {
                path: '/feed',
                name: 'Feed',
                component: feedComponent,
                meta: {
                    keepAlive: false
                }
            },
            {
                path: '/sessionSelf',
                name: 'session-instance',
                component: sessionSelfComponent,
                meta: {
                    keepAlive: false
                }
            }
        ]
        let whiteList = ['/login','/register', 'manager']
        let router = new VueRouter({
            mode: 'hash',
            routes: routers
        })
        router.beforeEach(async (to, from, next) => {
            // start progress bar
            // NProgress.start()
            // set page title
            // document.title = getPageTitle(to.meta.title)
            // determine whether the user has logged in
            const hasToken = user.getToken()
            console.log(hasToken)
            if (hasToken) {
                if (to.path === '/login') {
                    // if is logged in, redirect to the home page
                    next({ path: '/' })
                } else {
                        // 判断当前用户是否已拉取完user_info信息
                    if(!user.user){
                        user.getUserInfo().then(() => {
                            next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
                        }).catch(err => {
                            user.logout().then(()=>{
                                next({ path: '/' })
                            })
                        })
                    } else {
                        next()
                    }

                }
            } else {
                /* has no token*/
                console.log('has no token')
                if (whiteList.indexOf(to.path) !== -1) {
                    // in the free login whitelist, go directly
                    console.log('in the free login whitelist, go directly')
                    next()
                } else {
                    // other pages that do not have permission to access are redirected to the login page.
                    next(`/login?redirect=${to.path}`)
                }
            }
        })
        let app = new Vue({
            created() {

            },
            template: `<router-component></router-component> `,
            data() {
                return {}
            },
            el: '#App',
            router,
            components: {
                routerComponent
            }
        })

}


init()
